﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;
using System.Threading.Tasks;


namespace FaceSDKCShape
{

    class Program
    {
 
        static void Main(string[] args)
        {
            ///////////获取设备机器码提供给中运科技进行解码/////////////
            Byte[] pollcode = Enumerable.Repeat((Byte)0x00, 129).ToArray();
            Byte[] mac = Enumerable.Repeat((Byte)0x00, 129).ToArray();
            Byte[] cardstype = Enumerable.Repeat((Byte)0x00, 129).ToArray();
            int ret = ZYFaceSDK.ZY_GetDevicesSerialNumber(pollcode, mac,cardstype);
            if (ret < 0)
            {
                Console.WriteLine("Get PollCode error {0}!", ret);
                return;
            }
            else
            {
                Console.WriteLine("PollCode is {0} !",  Encoding.UTF8.GetString(pollcode));
                Console.WriteLine("mac is {0} !", Encoding.UTF8.GetString(mac, 0, 129));
                Console.WriteLine("cardstype is {0} !", Encoding.UTF8.GetString(cardstype, 0, 129));
            }

            ///////////////往下为功能性函数////////////
            string licensestr = "DhhezqVv+hyqVaovtSusVSudD3upV4/eihlLVbG0UgWpY6UpImcYQHV4Uof6T=";//对机器码解码后的授权码

            Byte[] licensecode = System.Text.Encoding.Default.GetBytes(licensestr);

            Byte[] decryptSerial = Enumerable.Repeat((Byte)0x00, 128).ToArray();
            Array.Copy(licensecode, decryptSerial, licensecode.Length);
            //初始化SDK
            short nChannel = 1;//SDK初始化通道数量 //初始化函数只需要初始化一次
            ret = ZYFaceSDK.ZY_FaceSDKInit(nChannel, decryptSerial);//第一个参数为要使用多少个检测器，应对不同线程调用，最多支持8路，内部以及做好线程安全的保护
            if (ret != 0)
            {
                Console.WriteLine("ZY_FaceSDKInit failed {0}!", ret);
                return;
            }
            string image_1stpath = "1.png";

            //读取一张图片
            Mat image_1st = CvInvoke.Imread(image_1stpath);
            Byte[] feature = Enumerable.Repeat((Byte)0x00, 512).ToArray();//实现分配保存人脸特征的数组 512为长度
            int flag = ZYFaceSDK.ZY_DetectAndExtract(image_1st.DataPointer, image_1st.Width, image_1st.Height, 3,feature);//获取特征，输入的图像中最多只包含一个人脸
            if (flag<0)
            {
                Console.WriteLine("ZY_DetectAndExtract failed {0}!", ret);
            }
            Console.WriteLine("ZY_DetectAndExtract sucess {0}!", ret);

            float score = ZYFaceSDK.ZY_FFCompara(feature, feature);
            Console.WriteLine("ZY_FFCompara {0}!", score);
            ZYFaceSDK.ZY_FaceSDKRelease();//释放，在程序退出来的时候释放


        }
    }
}
